package com.boot.security.server.dao.mffl;

import java.util.List;
import java.util.Map;

import com.boot.security.server.model.mffl.MfflContract;
import com.boot.security.server.model.mffl.MfflOrder;
import com.boot.security.server.model.mffl.Vo.MfflLawyerletterSystemVo;
import com.boot.security.server.model.mffl.Vo.MfflLawyerletterVo;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.boot.security.server.model.mffl.MfflLawyerletter;

/**
 * 律师函dao
 */
@Mapper
public interface MfflLawyerletterDao {

    @Select("select * from mffl_lawyerletter t where t.id = #{id} AND t.delete_state = 1")
    MfflLawyerletterVo getById(Long id);

    @Select("select * from mffl_lawyerletter t where t.id = #{id}")
    MfflLawyerletterVo getByIdSystem(Long id);

    @Delete("update mffl_lawyerletter set delete_state = 0,deleteTime = now() where id = #{id}")
    int delete(Long id);

    int update(MfflLawyerletter mfflLawyerletter);

    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into mffl_lawyerletter(typeId, serviceId, `describe`, imageUrl, Realname, number, payId, payNumber, payMerchantNumber, serviceLawyerId, payState, state, file, createTime, updateTime, delete_state, deleteTime, We, otherParty, content, addRessee, recipientPhoneNumber, address, courierCompanyId, courierNumber, Sender, originatingPlace, SenderPhone, paymentTime, money, userId, confirmTime, RefuseTime, commentState) values(#{typeId}, #{serviceId}, #{describe}, #{imageUrl}, #{Realname}, #{number}, #{payId}, #{payNumber}, #{payMerchantNumber}, #{serviceLawyerId}, #{payState}, #{state}, #{file}, #{createTime}, #{updateTime}, #{deleteState}, #{deleteTime}, #{We}, #{otherParty}, #{content}, #{addRessee}, #{recipientPhoneNumber}, #{address}, #{courierCompanyId}, #{courierNumber}, #{Sender}, #{originatingPlace}, #{SenderPhone}, #{paymentTime}, #{money}, #{userId}, #{confirmTime}, #{RefuseTime}, #{commentState})")
    int save(MfflLawyerletter mfflLawyerletter);

    int count(@Param("params") Map<String, Object> params);

    List<MfflLawyerletter> list(@Param("params") Map<String, Object> params, @Param("offset") Integer offset, @Param("limit") Integer limit);


    /**
     * 获取全部 律师已发送 律师函的订单
     * @return
     */
    @Select("select * from mffl_lawyerletter WHERE state = 2 AND delete_state = 1 AND payState = 1 AND confirmTime < NOW()")
    List<MfflLawyerletter> getAll();

    /**
     * 获取全部 律师未接单 律师函的订单
     * @return
     */
    @Select("select * from mffl_lawyerletter WHERE state <= 1 AND delete_state = 1 AND payState = 1 AND RefuseTime < NOW()")
    List<MfflLawyerletter> getRefuseAll();


    /**
     * 按照订单号查找律师函
     * @param paymerchantnumber
     * @return
     */
    @Select("select * from mffl_lawyerletter t where t.payMerchantNumber = #{paymerchantnumber} AND delete_state = 1")
    MfflOrder getBypaymerchantnumber(String paymerchantnumber);

    /**
     * 后台列表
     */
    int lawyerLetterCount(@Param("params") Map<String, Object> params);
    List<MfflLawyerletterSystemVo> lawyerLetterList(@Param("params") Map<String, Object> params, @Param("offset") Integer offset, @Param("limit") Integer limit);

    /**
     * 定时任务
     * 获取满足条件的数据（具体配置在mffl_service服务表中配置）
     *      1.用户发起律师函，律师未点击接收的
     *      2.律师点击完成服务，用户未点击确认完成的
     */
    @Select("SELECT a.id,a.userId,a.payMerchantNumber,a.serviceId,a.serviceLawyerId,a.state FROM mffl_lawyerletter as a ,mffl_order as b  WHERE a.payMerchantNumber = b.pay_merchant_number AND a.payState = b.state AND a.delete_state = 1 AND a.payState = 1 AND (a.state = 0 or a.state = 3) AND \n" +
            "IF(a.state = 0," +
            "(UNIX_TIMESTAMP()-UNIX_TIMESTAMP(a.paymentTime))*1000 > ${RefuseTime}," +
            "(UNIX_TIMESTAMP()-UNIX_TIMESTAMP(a.confirmTime))*1000 > ${ConfirmTime}) FOR UPDATE")
    List<MfflLawyerletter> getTimerTaskData(@Param("ConfirmTime") Long ConfirmTime,@Param("RefuseTime") Long RefuseTime);
}
